<?php
/**
 * 标签资讯列表页read more
 * 使用说明：需要传递自动参数  6个参数
 * limit
 * level
 * topTagId
 * dateType(天数范围) 传值2表示30天之内
 * rightnowreadmorecontroller.php
 * Create on 2015-04-25 15:00:12
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\lists;
use haibao\cms\business\control\ArticleController;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;

class RightNowReadMoreController extends \haibao\cms\business\control\pc\channel\ChannelController{
	

	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		if($this->checkData($controlData)){
			
			$this->parseTagId($controlData['manual']['topTagId']);
			$this->getMostReadData($controlData['manual']);
			// DO ： 获取推荐专题
			$featureList = array();
			if($controlData['manual']['isFeature']){
			    $feature = $exclusiveSerial = array();
			    $exclusive = \haibao\cms\business\control\FeatureController::getFeatureSerialArray();
			    if($exclusive){
			        foreach($exclusive as $key=>$exclusiveVal){
			            if(isset($exclusiveVal['children']) && !empty($exclusiveVal['children'])){
			                foreach($exclusiveVal['children'] as $k=>$val){
			                    $feature[$exclusiveVal['Id']][] = $val['Id'];
			                }
			            }
			        }
			        if($feature){
			            if($controlData['manual']['topTagId'] == 1383 || $controlData['manual']['topTagId'] == 7455 || $controlData['manual']['topTagId'] == 6863){
			                $featureList = $this->getExclusive($feature[47]);
			            }elseif($controlData['manual']['topTagId'] == 310 || $controlData['manual']['topTagId'] == 955){
			                $featureList = $this->getExclusive($feature[48]);
			            }elseif($controlData['manual']['topTagId'] == 24 || $controlData['manual']['topTagId'] == 520 || $controlData['manual']['topTagId'] == 211){
			                $featureList = $this->getExclusive($feature[49]);
			            }else{
			                $excluFirst = array();
			                $featureFirst = PublicControllerBusiness::getFeatureByChannel(0);
			                foreach($featureFirst as $k=>$val){
			                    $excluFirst['FeatureName'] = $val->FeatureDefine->FeatureName;
			                    $excluFirst['Url'] = $val->Url;
			                    $excluFirst['ImageUrl'] = $val->Image->Filename;
			                    $excluFirst['ListTime'] = $val->ListTime->format('Y-m-d H:i:s');
			                    $featureList[] = $excluFirst;
			                }
			            }
			        }
			        if($featureList){
			            $fans = array();
			            foreach($featureList as $key=>$val){
			                $fans[] = $val['ListTime'];
			            }
			            array_multisort($fans, SORT_DESC, $featureList);
			        }
			    }
			}
			$this->assign('featureList', $featureList);
			
			$path = $this->generateHtmlPath(__CLASS__,$controlData['manual']['topTagId']);
			$isNewHtml = $this->generateHtml($path,$content);
			$content = $this->getHtml($path);
			$content = $this->replaceCDNLink($content);
		}
		return $content;
	}
	
	private function getExclusive($serialIds){
	    $exclusive = $columList = $column = $featureList = $featureListAll = array();
	    if($serialIds){
	        foreach($serialIds as $serialId){
	            $featureList = \haibao\cms\business\control\FeatureController::getserialInfoById($serialId);
	            if($featureList){
	                $featureListAll[] = $featureList[0];
	            }
	        }
	    }
	    if($featureListAll){
	        foreach($featureListAll as $key=>$val){
	            $exclu['FeatureName'] = $val->FeatureDefine->FeatureName;
	            $exclu['Url'] = $val->Url;
	            $exclu['ImageUrl'] = $val->Image->Filename;
	            $exclu['ListTime'] = $val->ListTime->format('Y-m-d H:i:s');
	            $exclusive[] = $exclu;
	        }
	    }
	   
	    return  $exclusive;
	}
	
	/**
	 * 调用拥有该?标签，前?天浏览量最高，?级及以上的?条资讯和图集
	 */
	public function getMostReadData($dataCondition){
		$dataCondition['source'] = __CLASS__;
		$dataCondition['isProImage'] = true;
		$dataCondition['isDate'] = true;
		$dataCondition['dateType'] = $dataCondition['dateType'];
		
		$dataCondition['isTag'] = true;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['isPrimaryTag'] = true;
		$dataCondition['isParentTag'] = true;
		
		$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FOUR);

		if($this->conditionTagId == \haibao\cms\business\control\PublishController::NO_STRUCTURE_TAG){
			$dataCondition['isConditionTag'] = false;
		}else{
			$dataCondition['isConditionTag'] = true;
		}
		if($this->conditionTagId != \haibao\cms\business\control\PublishController::NO_STRUCTURE_TAG){
			$dataCondition['level'] = 5;
			$dataCondition['biglevel'] = 7;
		}else{
			$dataCondition['level'] = 6;
			$dataCondition['biglevel'] = 6;
		}
		list($articles,$imageColls) = $this->getArticleControl(2,$dataCondition);
		
		$mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
		
		$mostReadData = array();
		foreach($mergeData as $data){
			$temp = array();
			if($data->SiteType == ArticleController::INFO_TYPE_ARTICLE){
				
				if(isset($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
					$temp['ArticleProImage'] = $articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
				}else{
					if(isset($articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
						$temp['ArticleProImage'] = $articles['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
					}else{
						$temp['ArticleProImage'] = '';
					}
				}
				$temp['TagName'] = $articles['tag'][$data->ArticleTag->TagId]['TagName'];
				$temp['TagUrl'] = $articles['tag'][$data->ArticleTag->TagId]['Url'];
				$temp['Channel'] = self::getArticleUrl($data->PublishId,$data->ArticleTag->ParentId);
			}else{

				if(isset($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
					$temp['ArticleProImage'] = $imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
				}else{
					if(isset($imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
						$temp['ArticleProImage'] = $imageColls['proImage'][$data->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
					}else{
						$temp['ArticleProImage'] = '';
					}
				}
				$temp['TagName'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['TagName'];
				$temp['TagUrl'] = $imageColls['tag'][$data->ImageCollectiveTag->TagId]['Url'];
				$temp['Channel'] = self::getArticleUrl($data->Id,$data->ImageCollectiveTag->ParentId,ArticleController::INFO_TYPE_IMAGE);
				
			}

			$temp['Id'] = $data->Id;
			$temp['Title'] = $data->Title;
			$temp['ShortTitle'] = $data->ShortTitle?$data->ShortTitle:$data->Title;
			$temp['SiteType'] = $data->SiteType;
			array_push($mostReadData,$temp);
		}
		$this->assign('mostReadData',$mostReadData);
	}
	
	private function checkData($controlData){
		
		if($controlData['manual']){
			
			if(isset($controlData['manual']['limit']) &&
			isset($controlData['manual']['level']) &&
			isset($controlData['manual']['topTagId']) &&
			isset($controlData['manual']['dateType'])
			
			){
				return true;
				
			}else{
				return false;
			}
		}
	}
	
}